package com.wantupai.mvp.framework.lce.impl.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;


import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wantupai.mvp.R;
import com.wantupai.mvp.framework.base.adapter.BaseRecyclerAdapter;
import com.wantupai.mvp.framework.base.decoration.impl.Horizontaion_PaddingLeft_Right_DividerItemDecoration;
import com.wantupai.mvp.framework.lce.MvpLceView;
import com.wantupai.mvp.mvp.MvpPresenter;

import java.util.List;


/**
 * @param <D>
 * @param <V>
 * @param <P>
 * @author zhangqing
 */
public abstract class BaseRefreshLceFragment<D, AdApter extends BaseRecyclerAdapter<Item>, Item, V extends MvpLceView<D>, P extends MvpPresenter<V>> extends BaseMvpLceFragment<D, V, P>
{

    private boolean isDownRefresh = true;
    protected AdApter recyclerAdapter;
    private RecyclerView recyclerView;
    private RefreshLayout xRefreshView;

    public void refreshData(boolean refresh)
    {
        this.isDownRefresh = refresh;
        loadData(true);
    }

    public void setRefresDate(boolean isDownRefresh)
    {
        this.isDownRefresh = isDownRefresh;
    }

    @Override
    public void initContentView(View contentView)
    {
        super.initContentView(contentView);
        xRefreshView = (RefreshLayout) contentView.findViewById(R.id.xrefreshview);
        recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerview);
        if (hasItemDecoration())
        {
            recyclerView.addItemDecoration(getDividerItemDecoration());
        }
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(getLayoutManager());
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        //绑定Adapter
        recyclerAdapter = bindAdapter();
        recyclerView.setAdapter(recyclerAdapter);
        xRefreshView.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener()
        {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout)
            {
                refreshData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout)
            {
                refreshData(true);
            }
        });
    }

    @Override
    public void refreshComplete()
    {
        super.refreshComplete();
        getxRefreshView().finishRefresh();
    }

    @Override
    public void bindData(D data)
    {
        super.bindData(data);
        showContent();
        getAdapter().refreshAdapter(isDownRefresh(), convert(data));
    }

    @Override
    public void showError()
    {
        super.showError();
        setEnableRefresh(false);
        setEnableLoadMore(false);
        getxRefreshView().finishRefresh();
    }

    @Override
    public void showEmpty()
    {
        super.showEmpty();
        setEnableRefresh(true);
        setEnableLoadMore(false);
        getxRefreshView().finishRefresh();
    }


    public abstract List<Item> convert(D d);

    public boolean isDownRefresh()
    {
        return isDownRefresh;
    }

    public abstract AdApter bindAdapter();

    protected final AdApter getAdapter()
    {
        return recyclerAdapter;
    }

    public RecyclerView.LayoutManager getLayoutManager()
    {
        return new LinearLayoutManager(getActivity());
    }

    protected boolean hasItemDecoration()
    {
        return true;
    }

    public RecyclerView.ItemDecoration getDividerItemDecoration()
    {
        return new Horizontaion_PaddingLeft_Right_DividerItemDecoration(this);
    }

    protected final RecyclerView getRecyclerView()
    {
        return recyclerView;
    }

    public RefreshLayout getxRefreshView()
    {
        return xRefreshView;
    }

    public void refresh()
    {
        int count = getAdapter().getCount();
        if (count > 0)
        {
            getRecyclerView().post(() ->
            {
                getRecyclerView().scrollToPosition(0);
                getxRefreshView().autoRefresh();
            });
        }
        else
        {
            initData();
        }
    }

    @Override
    public void noMoreData()
    {
        super.noMoreData();
        setEnableLoadMore(false);
    }

    public void setEnableRefresh(boolean enableRefresh)
    {
        getxRefreshView().setEnableRefresh(enableRefresh);
    }

    public void setEnableLoadMore(boolean enableLoadMore)
    {
        getxRefreshView().setEnableLoadMore(enableLoadMore);
    }

    @Override
    public void loadMoreError()
    {
        super.loadMoreError();
        getxRefreshView().finishLoadMore();
    }

    public void setEnableRefreshAndLodeMore(boolean enableRefreshAndLodeMore)
    {
        setEnableLoadMore(enableRefreshAndLodeMore);
        setEnableRefresh(enableRefreshAndLodeMore);
    }
}
